package com.example.demo.controller;

import com.example.demo.DTO.LoginDto.LoginDto;
import com.example.demo.entity.ExamHistory;
import com.example.demo.entity.User;
import com.example.demo.entity.WrongQuestion;
import com.example.demo.jwt.Jwt;
import com.example.demo.response.Response;
import com.example.demo.service.ExamHistoryService;
import com.example.demo.service.UserService;
import com.example.demo.service.WrongQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags="用户控制器")
@RequestMapping(path="/user")
@NoArgsConstructor
@RestController
public class UserController {
  @Autowired
  private UserService userService;
  @Autowired
  private ExamHistoryService examHistoryService;
  @Autowired
  private WrongQuestionService wrongQuestionService;

  @ApiOperation("用户登录")
  @RequestMapping(path="/login",method = RequestMethod.POST)
  public Response login (@RequestBody LoginDto loginDto){
    String token = userService.login(loginDto);
    if(token == ""){
      return new Response(400,token,"不成功");
    }
    else{
      return new Response(200,token,"成功");
    }
  }

  @ApiOperation("获取用户信息")
  @RequestMapping(path="/info",method = RequestMethod.GET)
  public Response info (@RequestHeader("token") String token){
    Response res = new Response(400,"","不成功");
    if(token == ""){
      res.setCode(500);
      res.setMessage("没有token");
      return res;
    }
    User data = userService.getUserInfo(token);
    res.setCode(200);
    res.setData(data);
    res.setMessage("成功");
    return res;
  }

  @ApiOperation("添加用户")
  @RequestMapping(path="/register",method = RequestMethod.POST)
  public Response register (@RequestBody User user){
    String password = user.getUserPassword();
    user.setUserPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
    int isExcute = 0;
    try{
      isExcute = userService.register(user);
    }
    catch (Exception e){
      System.out.println(e.toString());
      return new Response(400,isExcute,e.toString());
    }

    return new Response(200,isExcute,"成功");
  }

  @ApiOperation("获取该用户考试记录数量")
  @RequestMapping(path="/exam-count",method = RequestMethod.GET)
  public Response examCount(@RequestHeader("token") String token){
    Response res = new Response(500,"","token不存在");
    if(token == ""){
      return res;
    }
    String userId = Jwt.decode(token);
    try{
      res.setData(examHistoryService.getUserExamHistoryCount(userId));
    }catch (Exception e){
      res.setCode(400);
      res.setMessage("不成功");
    }
    res.setCode(200);
    res.setMessage("成功");
    return res;
  }

  @ApiOperation("获取该用户错题记录数量")
  @RequestMapping(path="/wrong-count",method = RequestMethod.GET)
  public Response wrongCount(@RequestHeader("token") String token){
    Response res = new Response(500,"","token不存在");
    if(token == ""){
      return res;
    }
    String userId = Jwt.decode(token);
    try{
      res.setData(wrongQuestionService.getWrongQuestionCount(userId));
    }catch (Exception e){
      res.setCode(400);
      res.setMessage("不成功");
    }
    res.setCode(200);
    res.setMessage("成功");
    return res;
  }
  @ApiOperation("获取考试记录")
  @RequestMapping(path="/exam-history",method = RequestMethod.GET)
  public Response exam_history(@RequestHeader("token") String token){
    Response res = new Response(500,"","没有token");
    if(token == ""){
      return res;
    }
    String userId = Jwt.decode(token);
    try{
      res.setData(examHistoryService.getUserExamHistory(userId));
    }catch(Exception e){
      System.out.println(e.toString());
      res.setMessage(e.toString());
    }
    res.setCode(200);
    res.setMessage("成功");
    return res;
  }

  @ApiOperation("获取错题")
  @RequestMapping(path="/wrong-question-list",method = RequestMethod.GET)
  public Response wrongQuestionList(@RequestHeader("token") String token){
    Response res = new Response(500,"","没有token");
    if(token == ""){
      return res;
    }
    String userId = Jwt.decode(token);
    try{
      res.setData(wrongQuestionService.getWrongQuestion(userId));
    }catch (Exception e){
      res.setCode(400);
      res.setMessage(e.toString());
    }
    res.setCode(200);
    res.setMessage("成功");
    return res;
  }


}
